package com.after.saas.common.utils;


import cn.hutool.core.util.StrUtil;
import com.after.saas.common.constants.Constants;
import com.after.saas.common.constants.JwtEnum;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author hjchen
 * @Description: 获取请求
 * @date 2021-5-7
 */

public abstract class RequestUtil {

    public static HttpServletRequest getRequest() {
        if (RequestContextHolder.getRequestAttributes() != null) {
            return ((ServletRequestAttributes) (RequestContextHolder.getRequestAttributes())).getRequest();
        }
        return null;
    }

    public static ResponseEntity postUtil(RestTemplate restTemplate, String url, HttpEntity requestEntity) {
        return restTemplate.exchange(url, HttpMethod.POST, requestEntity, Object.class);
    }

    public static HttpHeaders buildHeader() {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.add(JwtEnum.WEB_FRONT.getTokenName(), Constants.TOKEN_PREFIX + RequestUtil.getToken());
        return httpHeaders;
    }

    public static HttpHeaders buildHeader(Map<String, String> headers) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            httpHeaders.add(entry.getKey(), entry.getValue());
        }
        return httpHeaders;
    }

    public static HttpHeaders buildHeaders(HttpServletRequest request) {
        HttpHeaders headers = new HttpHeaders();
        Map<String, String> headersInfo = getHeadersInfo(request);
        for (Map.Entry<String, String> entry : headersInfo.entrySet()) {
            headers.add(entry.getKey(), entry.getValue());
        }
        return headers;
    }

    /**
     * 获取登录请求header信息
     *
     * @return
     */
    public static String getLoginRequestHeaderInfo(String key) {
        HttpServletRequest request = getRequest();
        String loginToken = request.getHeader(key);
        if (request.getMethod().equals(HttpMethod.GET.name())) {
            if (StrUtil.isEmpty(loginToken)) {
                loginToken = request.getParameter(key);
            }
        }
        if (StrUtil.isNotEmpty(loginToken) && loginToken.startsWith(Constants.TOKEN_PREFIX)) {
            loginToken = loginToken.substring(Constants.TOKEN_PREFIX.length());
        }
        return loginToken;
    }

    /**
     * 获取请求header
     *
     * @param request
     * @return
     */
    public static Map<String, String> getHeadersInfo(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }
        return map;
    }

    /**
     * 根据request获取请求token
     */
    public static String getToken() {
        String token = getRequest().getHeader(Constants.TOKEN_HEADER);
        if (StrUtil.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) {
            token = token.substring(Constants.TOKEN_PREFIX.length());
        }
        return token;
    }
}
